﻿/**
<code>
  <author name="Zealic" email="zealic(at)gmail.com" type="Creator"/>
</code>
**/
using System;
using System.Collections.Generic;


namespace Zealib.Transform
{
    /// <summary>
    /// Transform extensions
    /// </summary>
    public static class TransformExtensions
    {
        /// <summary>
        /// Check can source transform as <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TDestination">Destination type.</typeparam>
        /// <param name="self">Extened object.</param>
        /// <returns>If can transform return true.</returns>
        public static bool CanTransformTo<TDestination>(this object self)
        {
            if (self == null) throw new ArgumentNullException("self");
            ITransformer transformer = TransformManager.GetTransformer(self.GetType());
            return transformer.CanTransformTo<TDestination>();
        }

        /// <summary>
        /// Check can transform destiation as <typeparamref name="TSource"/>.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <param name="self">Extened object.</param>
        /// <returns>If can transform return true.</returns>
        public static bool CanTransformBack<TSource>(this object self)
        {
            if (self == null) throw new ArgumentNullException("self");
            ITransformer transformer = TransformManager.GetTransformer(self.GetType());
            return transformer.CanTransformBack<TSource>();
        }

        /// <summary>
        /// Transform source as <typeparamref name="TDestination"/>.
        /// </summary>
        /// <typeparam name="TDestination">Destination type.</typeparam>
        /// <param name="self">Source object.</param>
        /// <returns>Transformed destination object.</returns>
        public static TDestination TransformTo<TDestination>(this object self)
        {
            if (self == null) throw new ArgumentNullException("self");
            ITransformer transformer = TransformManager.GetTransformer(self.GetType());
            return transformer.TransformTo<TDestination>(self);
        }

        /// <summary>
        /// Transform destiation as <typeparamref name="TSource"/>.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <param name="self">Destiation object.</param>
        /// <returns>Transformed source object.</returns>
        public static TSource TransformBack<TSource>(this object self)
        {
            if (self == null) throw new ArgumentNullException("self");
            ITransformer transformer = TransformManager.GetTransformer(typeof(TSource));
            return transformer.TransformBack<TSource>(self);
        }

        public static List<TDestination> TransformToList<TSource, TDestination>(this IList<TSource> self)
        {
            if (self == null) throw new ArgumentNullException("self");
            List<TDestination> result = new List<TDestination>(self.Count);
            foreach (TSource source in self)
            {
                result.Add(source.TransformTo<TDestination>());
            }
            return result;
        }

        public static List<TSource> TransformBackList<TSource, TDestination>(this IList<TDestination> self)
        {
            if (self == null) throw new ArgumentNullException("self");
            List<TSource> result = new List<TSource>(self.Count);
            foreach (TDestination dest in self)
            {
                result.Add(dest.TransformBack<TSource>());
            }
            return result;
        }

    }

}
